package com.itheima.auth.handle;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.support.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e)
            throws IOException, ServletException {
        httpServletResponse.setCharacterEncoding("UTF-8");
        Result<String> result=Result.buildError("用户名或密码错误");
        ObjectMapper objectMapper=new ObjectMapper();
        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(result));

    }
}
